﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.AubitFile;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Domain.PM.select;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.Reflection.Metadata;
using static FarmSmartSleep.Organ.API.Application.Commands.SheepFarmCommands.SheepFarmSelectHandler;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.FemaleRen
{
    public class FemaleRenSelectHandler : IRequestHandler<FemaleRenSelectCommand, Result>
    {
        public readonly IBaseRepository<EwePregnancyCheckRecord> ewePregnancyCheckRecordRepository;
        public readonly IBaseRepository<PregnancyCheckMethod> pregnancyCheckMethodRepository;
        public readonly IBaseRepository<PregnancyTests> pregnancyTestsRepository;
        public readonly IBaseRepository<Cottages> cottagesRepository;
        public readonly IBaseRepository<Fields> fieldsRepository;
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;

        public FemaleRenSelectHandler(IBaseRepository<EwePregnancyCheckRecord> ewePregnancyCheckRecordRepository, IBaseRepository<PregnancyCheckMethod> pregnancyCheckMethodRepository, IBaseRepository<PregnancyTests> pregnancyTestsRepository, IBaseRepository<Cottages> cottagesRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<SleepInfor> sleepinforbaseRepository)
        {
            this.ewePregnancyCheckRecordRepository = ewePregnancyCheckRecordRepository;//种母妊检
            this.pregnancyCheckMethodRepository = pregnancyCheckMethodRepository;//结果
            this.pregnancyTestsRepository = pregnancyTestsRepository;//方式
            this.cottagesRepository = cottagesRepository;//栋舍
            this.fieldsRepository = fieldsRepository;//栏位
            this.sleepinforbaseRepository = sleepinforbaseRepository;
        }

        /// <summary>
        /// 母羊妊检记录查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FemaleRenSelectCommand request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CodeEnum.search };
            var data = new PageDto() { };

            var ewePregnancy = ewePregnancyCheckRecordRepository.GetAll().Where(x => x.Deletes == true);
            var pregnancyCheck = pregnancyCheckMethodRepository.GetAll();
            var pregnancyTest = pregnancyTestsRepository.GetAll();
            var cottages = cottagesRepository.GetAll();
            var fields = fieldsRepository.GetAll();
            var listQuery = (from s in ewePregnancy
                             join m in pregnancyCheck
                             on s.PregnancyTestMethod equals m.PregnancyCheckMethodId
                             join b in pregnancyTest
                             on s.PregnancyTestResult equals b.PregnancyTestResult
                             join v in cottages
                             on s.CurrentBarn equals v.CottagesId
                             join x in fields
                             on s.CurrentPen equals x.FieIdId
                             join v1 in cottages
                             on s.TransferBarn equals v1.CottagesId
                             join x1 in fields
                             on s.TransferPen equals x1.FieIdId
                             join d in sleepinforbaseRepository.GetAll()
                             on s.EarNo equals d.SleepInforId
                             select new EwePregnancyCheckRecordDto
                             {
                                 EwePregnancyCheckRecordId = s.EwePregnancyCheckRecordId,//序号
                                 PregnancyTestDate = s.PregnancyTestDate,//妊检日期
                                 EarNo = s.EarNo,                            
                                 PregnancyTestResult = s.PregnancyTestResult,//妊检结果ID
                                 PregnancyTestResultName = b.PregnancyTestResultName,//妊检结果
                                 PregnancyTestMethod = s.PregnancyTestMethod,//妊检方式ID
                                 PregnancyCheckMethodName = m.PregnancyCheckMethodName,//妊检方式
                                 LitterCount = s.LitterCount, //同胎数
                                 PregnancyDays = s.PregnancyDays,//妊振天数  
                                 CurrentBarn = v.CottageName,
                                 CurrentBarnId = v.CottagesId,
                                 CurrentPen = x.FieIdName,
                                 CurrentPenId = x.FieIdId,
                                 TransferBarn = v1.CottageName,
                                 TransferBarnId = v1.CottagesId,
                                 TransferPen = x1.FieIdName,
                                 TransferPenId = x1.FieIdId,
                                 Deletes = s.Deletes,//删除标识
                                 Leader = s.Leader,//负责人
                                 CreateId = s.CreateId,//创建人
                                 CreateTime = s.CreateTime,//创建时间
                                 UpdateId = s.UpdateId,//修改人
                                 UpdateTime = s.UpdateTime,//修改时间
                                 ExamId = s.ExamId,//审核人
                                 ExamTime = s.ExamTime,//审核时间
                                 DocumentNo = s.DocumentNo,//单据号
                                 EarNumber=d.EarNumber
                             }).AsQueryable();
            //种母耳号             
            if (request.EarNo != 0)
            {
                listQuery = listQuery.Where(x => x.EarNo == request.EarNo);
            }
            //妊检日期
            if (request.MinDate != null)
            {
                listQuery = listQuery.Where(x => x.PregnancyTestDate >= DateTime.Parse(request.MinDate));
            }
            if (request.MaxDate != null)
            {
                listQuery = listQuery.Where(x => x.PregnancyTestDate <= DateTime.Parse(request.MaxDate));
            }
            //负责人
            if (!string.IsNullOrEmpty(request.Leader))
            {
                listQuery = listQuery.Where(x => x.Leader.Contains(request.Leader));
            }
            //妊检结果
            if (request.PregnancyTestResult != 0)
            {
                listQuery = listQuery.Where(x => x.PregnancyTestResult == request.PregnancyTestResult);
            }
            //同胎数
            if (request.LitterCount != 0)
            {
                listQuery = listQuery.Where(x => x.LitterCount == request.LitterCount);
            }
            data.TotalCount = listQuery.Count();
            data.PageCount = (int)Math.Ceiling((listQuery.Count() * 1.0) / request.PageSize);
            data.data = listQuery.OrderBy(x => x.EwePregnancyCheckRecordId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            res.Code = (int)CodeEnum.success;
            res.Data = data;
            return res;


        }
        public class PageDto
        {
            public int PageCount { get; set; }
            public int TotalCount { get; set; }
            public List<EwePregnancyCheckRecordDto> data { get; set; }

        }
    }
}
